package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    /**
     * 列表搜索
     *
     * @param searchParam
     * @return
     */
    @GetMapping("list.html")
    public String search(SearchParam searchParam, Model model) {

        Result<Map> searchData = listFeignClient.list(searchParam);

        model.addAllAttributes(searchData.getData());
//            拼接请求路径?category3Id=61&trademark=2:华为&props=23:4G:运行内存&order=1:desc
        String pathUrls = makePathUrls(searchParam);

//        制作平台属性面包屑
        String trademarkParam = makeTrademark(searchParam.getTrademark());
//        制作平台属性和平台属性值面包屑
        List<Map> mapList = makeProps(searchParam.getProps());
//        制作排序功能
        Map<String, String> makeSort = makeSort(searchParam.getOrder());


//        添加模型数据
        model.addAttribute("urlParam", pathUrls);
        model.addAttribute("searchParam", searchParam);
        model.addAttribute("propsParamList", mapList);
        model.addAttribute("orderMap", makeSort);
        model.addAttribute("trademarkParam",trademarkParam);

//            返回页面数据
        return "list/index";
    }

    //        制作平台属性面包屑
    private String makeTrademark(String trademark) {
//        品牌:2,品牌名称     品牌：华为
        if (StringUtils.isNotEmpty(trademark)) {
            String[] split = trademark.split(":");
            if (split != null && split.length == 2) {
                return "品牌:" + split[1];
            }

        }
        return "";
    }

    //            拼接请求路径
    private String makePathUrls(SearchParam searchParam) {

//        字符拼接
        StringBuilder sp = new StringBuilder();

//        判断是否为全文搜索
        if (StringUtils.isNotEmpty(searchParam.getKeyword())) {
//            拼接路径
//  http://list.gmall.com/list.html?category3Id=61&props=1:2800-4499:%E4%BB%B7%E6%A0%BC&order=
            sp.append("keyword=").append(searchParam.getKeyword());
        }
//        判断是否从分类id进行查询
        if (StringUtils.isNotEmpty(searchParam.getCategory1Id())) {
            sp.append("category1Id=").append(searchParam.getCategory1Id());
        }
        if (StringUtils.isNotEmpty(searchParam.getCategory2Id())) {
            sp.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (StringUtils.isNotEmpty(searchParam.getCategory3Id())) {
            sp.append("category3Id=").append(searchParam.getCategory3Id());
        }

//            判断是否从属性值查询
        if (StringUtils.isNotEmpty(searchParam.getTrademark())) {
            if (sp.length() > 0) {
                sp.append("&trademark=").append(searchParam.getTrademark());
            }
        }

//        判断是否从属性值名称获取
        if (searchParam.getProps() != null && searchParam.getProps().length > 0) {
            for (String prop : searchParam.getProps()) {
                sp.append("&props=").append(prop);
            }
        }

//        返回路径值
        return "list.html?" + sp.toString();
    }

    //        制作平台属性和平台属性值面包屑
    private List<Map> makeProps(String[] props) {

        List<Map> mapList = new ArrayList<>();

        if (props != null && props.length > 0) {
//            props=1:2800-4499:价格&props=2:6.85-6.94英寸:屏幕尺寸
            for (String prop : props) {
                String[] split = prop.split(":");
                if (split != null && split.length > 0) {
                    Map<String, String> map = new HashMap<>();
                    map.put("attrId", split[0]);
                    map.put("attrValue", split[1]);
                    map.put("attrName", split[2]);
                    mapList.add(map);
                }
            }
        }
        return mapList;
    }

    //        制作排序功能
    private Map<String, String> makeSort(String order) {
        Map<String, String> map = new HashMap<>();
        if (StringUtils.isNotEmpty(order)) {
            String[] split = order.split(":");
            if (split != null && split.length > 0) {
                map.put("type", split[0]);
                map.put("sort", split[1]);
                return map;
            }
        }
        //            给默认排序
        map.put("type", "1");
        map.put("sort", "asc");
        return map;
    }
}
